#ifndef __MEMORY__
#define __MEMORY__

#include <grub.h>

/*
	1 Seite = 4Kb
	MaxSpeicher: 4GB = 4194304Kb dividiert durch 4Kb(eine Seite) = 1048576 ansprechbare Seiten
	1Byte(unsigned char) = 8Bit -> also kann man acht Seiten in einem char unterbringen
	des is doch gleich: 1048576 / 8 = 131072 Einträge
*/

#ifndef TRUE
#define TRUE  1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#define MEM_MAP_ENTRIES 131072

#define RESERVED 0x0
#define FREE     0x1

#define SET |=
#define UNSET &= ~

#define MEMORY_PRESENT      0x1
#define PAGE_WRITEABLE      0x2
#define EVERYONE_ACCESSABLE 0x4
#define WRITE_THROUGH_ON 	 0x8
#define CACHING_OFF			 0x10

/*0x100000 == 1MB*/
#define HEAP_START 0x900000
#define HEAP_END   0xE00000
#define HEAP_SIZE  0x500000
#define AVIABLE_VARIABLES 1000
#define TASK_SPACE 0x200000
#define KERNEL_START 0x500000

struct kernelPagingStructure {
    unsigned int *pageDirectory;
    unsigned int *pageTable[1024];
    unsigned short aktPageDirectory;
    unsigned short aktPageTable;
    unsigned int realAddr;
};

void initMemoryMap();
void markMemoryFree(unsigned int addr);
void *getFreeMemory();
void initPhysMemManagement(struct multibootInfo *mbInfo);
void initPaging();
void ladePageTable();
void initHeap();
void pageFaultException(unsigned long error, unsigned long eip);
void mapPage(unsigned int physMemory, struct kernelPagingStructure *str);
unsigned int alloc(unsigned int Size);
unsigned int copyProgramToVAddrRoom(unsigned int progstart, int size, int off);
struct kernelPagingStructure makePageDirForTask();
struct kernelPagingStructure getKernelPagingStruct();
void addTaskCount();
void memDump(unsigned int physMemory, struct kernelPagingStructure *str);

#endif
